import 'package:shared_preferences/shared_preferences.dart';

extension StringExt on String {
  void putStore(dynamic value) {
    StoreUtils.setAny(this, value);
  }

  String getStore({String defaultValue = ""}) => StoreUtils.getString(this) ?? defaultValue;

  int getIntStore({int defaultValue = 0}) => StoreUtils.getInt(this) ?? defaultValue;

  bool getBoolStore({bool defaultValue = false}) => StoreUtils.getBool(this) ?? defaultValue;

  double getDoubleStore({double defaultValue = 0}) => StoreUtils.getDouble(this) ?? defaultValue;
}

class StoreUtils {
  StoreUtils._();

  static SharedPreferences? _prefs;
  static final Map<String, dynamic> _cache = {};

  /// 初始化
  static Future<void> init() async {
    await get();
    await loadCache();
  }

  static Future<void> get() async {
    _prefs ??= await SharedPreferences.getInstance();
  }

  /// 加载缓存
  static Future<void> loadCache() async {
    _cache.clear();
    for (String key in _prefs!.getKeys()) {
      _cache[key] = _prefs!.get(key);
    }
  }

  /// 获取字符串
  static String? getString(String key) {
    return _cache[key] as String? ?? _prefs!.getString(key);
  }

  /// 设置字符串
  static Future<bool> setString(String key, String value) async {
    _cache[key] = value;
    return await _prefs!.setString(key, value);
  }

  /// 获取整数
  static int? getInt(String key) {
    return _cache[key] as int? ?? _prefs!.getInt(key);
  }

  /// 设置整数
  static Future<bool> setInt(String key, int value) async {
    _cache[key] = value;
    return await _prefs!.setInt(key, value);
  }

  /// 获取浮点数
  static double? getDouble(String key) {
    return _cache[key] as double? ?? _prefs!.getDouble(key);
  }

  /// 设置浮点数
  static Future<bool> setDouble(String key, double value) async {
    _cache[key] = value;
    return await _prefs!.setDouble(key, value);
  }

  /// 获取布尔值
  static bool? getBool(String key) {
    return _cache[key] as bool? ?? _prefs!.getBool(key);
  }

  /// 设置布尔值
  static Future<bool> setBool(String key, bool value) async {
    _cache[key] = value;
    return await _prefs!.setBool(key, value);
  }

  /// 获取字符串列表
  static List<String>? getStringList(String key) {
    return _cache[key] as List<String>? ?? _prefs!.getStringList(key);
  }

  /// 设置字符串列表
  static Future<bool> setStringList(String key, List<String> value) async {
    _cache[key] = value;
    return await _prefs!.setStringList(key, value);
  }

  /// 删除键值对
  static Future<bool> remove(String key) async {
    _cache.remove(key);
    return await _prefs!.remove(key);
  }

  /// 清空所有数据
  static Future<bool> clear() async {
    _cache.clear();
    return await _prefs!.clear();
  }

  /// 批量设置键值对
  static Future<bool> setAny(String key, dynamic value) async {
    switch (value.runtimeType) {
      case String _:
        await setString(key, value);
        break;
      case int _:
        await setInt(key, value);
        break;
      case double _:
        await setDouble(key, value);
        break;
      case bool _:
        await setBool(key, value);
        break;
      case const (List<String>):
        await setStringList(key, value);
        break;
      case Map _:
        for (MapEntry entry in value.entries) {
          await setAny(entry.key, entry.value);
        }
        break;
      default:
        throw UnsupportedError('Unsupported data type: ${value.runtimeType}');
    }
    return true;
  }
}
